package hotel.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 


import org.apache.log4j.Logger;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
 * Change language
 * 
 *  @author Alex
 */
public class LanguageSelectAction extends Action
{
	private static final Logger logger = Logger.getLogger(LanguageSelectAction.class);
	private static final String LANGUAGESELECTACTION_ERROR = "Error in LanguageSelectAction:";
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) 
			throws Exception 
	{
		String lang = request.getParameter("lang");
		String mapper = null;
		
		try
		{
			if (!"en".equals(lang.toLowerCase()) 
				&& !"ru".equals(lang.toLowerCase())) {
				
				lang = "en";
			}
			
			Locale locale = new Locale(lang);
	        mapper = request.getHeader("Referer");
	        request.getSession(true).setAttribute(Globals.LOCALE_KEY, locale);
	        response.setLocale(locale);
	        
	        if (mapper == null) 
	        {
	        	mapper = "index.jsp";
	        }
		}
		catch(NullPointerException e)
		{
			logger.error(LANGUAGESELECTACTION_ERROR + e);
	 	}
        
        return new ActionForward(mapper, true);
	}
}
